home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_03_02 / 3n02051a < prev    next >
Text File  |  1991-12-21  |  2KB  |  48 lines

  1. // critsec.c example 2
  2. // cl /AL /Lp critsec.c
  3. // bind286 critsec
  4. // critsec
  5.  
  6. #define INCL_DOS
  7. #define INCL_SUB
  8. #include <os2.h>
  9. #include <malloc.h>        
  10. #define STACK_SIZE 4096    
  11.  
  12. main() 
  13.   char far *stkptr;        /* stack pointer for threads */
  14.   void far thread1();      /* prototype for thread 1    */ 
  15.   void far thread2();      /* prototype for thread 2    */ 
  16.   unsigned threadID;       /* thread ID number          */ 
  17.   int count;               /* loop counter              */
  18.   int i = 400;             /* note frequency            */
  19.   stkptr = (char far *)malloc(STACK_SIZE) + STACK_SIZE; /* get stack space  */ 
  20.   DosCreateThread(thread1, &threadID, stkptr);          /* start thread one */ 
  21.   stkptr = (char far *)malloc(STACK_SIZE) + STACK_SIZE; /* get stack space  */ 
  22.   DosCreateThread(thread2, &threadID, stkptr);          /* start thread two */ 
  23.   DosSleep(400L);    /* let other threads run */
  24.   DosEnterCritSec(); /* stop other threads    */
  25.   printf("\nEntered critical section");
  26.     for(count=0; count <1; count++) {          // beeping starts
  27.       for(; i < 800; i += 10)
  28.          DosBeep(i,50);
  29.       for(; i > 400; i -=5)
  30.          DosBeep(i,50);
  31.     }                          // beeping ends
  32.   printf("    Exited critical section\n");
  33.   DosExitCritSec(); /* start other threads again  */
  34.   DosSleep(400L);   /* let other threads continue */
  35.   exit(0);          /* exit all threads           */ 
  36.  
  37. void far thread1() {        /* thread1()              */ 
  38.    while(TRUE)              /* continuously print "1" */ 
  39.       VioWrtTTY("1", 1, 0);
  40.  
  41. void far thread2() {        /* thread2()              */
  42.    while(TRUE)              /* continuously print "2" */ 
  43.       VioWrtTTY("2", 1, 0);
  44. }
  45.